<template>
  <div class="wrap">
    <div class="search-wrap">
       <el-input v-model="search.userName" placeholder="模糊搜索用户名" style="width:350px" />
        <el-button
        class="search-class"
        type="primary"
            @click.stop="getUserList"
        >查询</el-button>
    </div>
    <el-table :data="userList" style="width: 100%">
      <el-table-column prop="userName" label="姓名"> </el-table-column>
      <el-table-column prop="nickName" label="昵称"> </el-table-column>
      <el-table-column prop="headPicture" label="头像">
        <template v-slot="scope">
          <img class="headPictureCol" :src="scope.row.headPicture" alt="" />
        </template>
      </el-table-column>
      <el-table-column label="角色">
        <template v-slot="scope">
          {{ scope.row.roles.map((role) => role.name).join("、") }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="120">
        <template v-slot="scope">
          <el-button
            @click.stop="userAddRole(scope.row)"
            type="primary"
            size="small"
          >
            分配角色
          </el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column fixed="right" label="操作" width="120">
        <template v-slot="scope">
          <el-button
            @click.stop="deleteRow(scope.row)"
            size="small"
          >
            删除
          </el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <el-dialog
      v-model="dialogFormVisible"
      :close-on-click-modal="false"
      title="授权"
    >
      <el-form :model="form" label-position="top">
        <el-form-item label="角色" label-width="140px">
          <el-checkbox-group v-model="form.roles">
            <el-checkbox
              :label="role.id"
              v-for="(role, index) in allRoleList"
              :key="index"
              >{{ role.name }}</el-checkbox
            >
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="confirm"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import request from "utils/request";
import { ElMessage, ElMessageBox } from "element-plus";

export default {
  name: "JddChromePluginsUserList",

  data() {
    return {
      dialogFormVisible: false,
      search:{
        userName:''
      },
      form: {
        id: "",
        roles: [],
      },
      userList: [],
      allRoleList: [],
    };
  },

  mounted() {
    this.getUserList();
  },

  methods: {
    async getUserList() {
      let ret = await request.get("/userList", {
        userName:this.search.userName
      });
      if (ret.code == 200) {
        this.userList = ret.data || [];
      }
    },
    async getAllRoleList() {
      let ret = await request.get("/getRoleList", {});
      if (ret.code == 200) {
        this.allRoleList = ret.data || [];
      }
    },
    async findUserSelfRoles(userId) {
      let ret = await request.get("/findUserSelfRoles", { userId });
      if (ret.code == 200) {
        this.form.roles = ret.data || [];
      }
    },
    async userAddRole(row) {
      this.form.id = row.id;
      this.getAllRoleList();
      this.findUserSelfRoles(row.id);
      this.dialogFormVisible = true;
    },
    async confirm() {
      let ret = await request.post("/userAddRole", {
        userId: this.form.id,
        roleIds: this.form.roles,
      });
      if (ret.code == 200) {
        this.$message({
          message: "修改成功",
          type: "success",
        });
        this.getUserList();
        this.dialogFormVisible = false;
      } else {
        this.$message({
          message: ret.message,
          type: "error",
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.wrap {
  .headPictureCol {
    width: 32px;
    height: 32px;
    overflow: hidden;
    border-radius: 50%;
  }
  .search-wrap{
    display: flex;
    margin-bottom: 15px;
    .search-class{
      margin-left: 20px;
    }
  }
}
</style>
